﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Citrix.XaAdminMobile.ViewModel;
using System.Diagnostics;

namespace Citrix.XaAdminMobile
{
    /// <summary>
    /// The main app window. Manages swapping between different layouts (phone and tablet).
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// The root view-model object - holds the page navigation state.
        /// </summary>
        private ViewModelManager vmRoot = null;

        /// <summary>
        /// Ctor.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Sets up the data context for the main window.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            this.vmRoot = App.TypedApp.ViewModel;
            this.vmRoot.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ViewModel_PropertyChanged);
            ApplyLayout();
        }

        /// <summary>
        /// Handle changes to a new layout.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ActiveNavigation")
            {
                ApplyLayout();
            }
        }

        /// <summary>
        /// Sets the window content to an instance of the appropriate layout.
        /// </summary>
        private void ApplyLayout()
        {
            if (this.vmRoot.ActiveNavigation is PhoneNavigation)
            {
                this.ContentTarget.Content = new ViewPhone.PhoneLayout();
            }
            else
            {
                this.ContentTarget.Content = new ViewTablet.TabletLayout();
            }

        }
    }
}
